#include "sylar/sylar.h"
#include <vector>
#include <unistd.h>

sylar::Logger::LoggerPtr g_logger = SYLAR_LOG_ROOT();

int count = 0;
sylar::RWMutex rwlock;
sylar::Mutex s_mutex;

void fun1()
{
    SYLAR_LOG_INFO(g_logger) << "thread_name: " << sylar::Thread::GetName()
                            << " this->name: " << sylar::Thread::GetThis()->getName()
                            << " id: " << sylar::GetThreadId()
                            << " this->id: " << sylar::Thread::GetThis()->getId(); 
    //sleep(20);
    for (int i = 0; i < 10000000; i++)
    {
        //sylar::RWMutex::WriteLock lock(rwlock);
        sylar::Mutex::Lock lock(s_mutex);
        ++count;
    }
}

void fun2() 
{
    while(true) {
        SYLAR_LOG_INFO(g_logger) << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    }
}

void fun3() 
{
    while(true) {
        SYLAR_LOG_INFO(g_logger) << "========================================";
    }
}

int main(int argc, char* argv[])
{
    std::vector<sylar::Thread::Ptr> vec;

    SYLAR_LOG_INFO(g_logger) << "thread is begin";
    YAML::Node root = YAML::LoadFile("../etc/log2.yaml");
    sylar::Config::LoadFromYaml(root);
    for (int i = 0; i < 2; i++)
    {
        sylar::Thread::Ptr p = std::make_shared<sylar::Thread>(&fun2, "name_" + std::to_string(i * 2));
        sylar::Thread::Ptr p2 = std::make_shared<sylar::Thread>(&fun3, "name_" + std::to_string(i * 2 + 1));
        vec.emplace_back(p);
        vec.emplace_back(p2);
    }

    for (int i = 0; i < (int)vec.size(); i++)
    {
        vec[i]->join();
    }

    SYLAR_LOG_INFO(g_logger) << "thread is end";
    SYLAR_LOG_INFO(g_logger) << "count " << count;
    return 0; 
}